﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using com.crawler.common;
using com.crawler.components;


namespace com.crawler.winfrom
{
    public partial class FrmSetting : Form
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (txt_link_string.Text.Trim() == "")
            {
                MessageBox.Show("数据库链接不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txt_chapter_txt_path.Text.Trim() == "")
            {
                MessageBox.Show("图书索引目录不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int t_thread_count = 10;
            int t_timeout = 20;

            if (!int.TryParse(txt_thread_count.Text.Trim(), out t_thread_count))
                t_thread_count = 0;

            if (t_thread_count == 0)
            {
                MessageBox.Show("线程数量不能小于0!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!int.TryParse(txt_timeout.Text.Trim(), out t_timeout))
                t_timeout = 0;

            if (t_timeout == 0)
            {
                MessageBox.Show("超时时间不能小于0!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            SysConfigInfo t_config = new SysConfigInfo();

            t_config.DBType = rdo_mysql.Checked ? Consts.DBType.MySQL : Consts.DBType.ServerSQL;
            t_config.DBLinkString = txt_link_string.Text.Trim();
            t_config.ThreadCount = t_thread_count;
            t_config.TimeOut = t_timeout;
            t_config.BookIndexPath = txt_chapter_txt_path.Text.Trim();
            t_config.ICONPath = txt_icon_path.Text.Trim();

            t_config.CrawlerType = cmb_crawler_type.Text=="目录"? 1:2;

            try
            {
                bool t_ret = SerializationHelper.Save(t_config, "");

                if (t_ret)
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("保存系统配置失败" + ex.ToString());
                MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// 窗体加载配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSetting_Load(object sender, EventArgs e)
        {
            try
            {
                SysConfigInfo t_config = SerializationHelper.Load(typeof(SysConfigInfo), "") as SysConfigInfo;

                if (t_config != null)
                {
                    if (t_config.DBType == Consts.DBType.MySQL)
                    {
                        rdo_mysql.Checked = true;
                    }
                    else
                    {
                        rdo_sqlserver.Checked = true;
                    }

                    txt_link_string.Text = t_config.DBLinkString;
                    txt_thread_count.Text = t_config.ThreadCount.ToString();
                    txt_timeout.Text = t_config.TimeOut.ToString();
                    txt_chapter_txt_path.Text = t_config.BookIndexPath;
                    txt_icon_path.Text = t_config.ICONPath;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载系统配置失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 选择目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_folder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txt_chapter_txt_path.Text = folderBrowserDialog1.SelectedPath.ToString();
            }
        }

        /// <summary>
        /// 初始化数据库脚本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_db_script_Click(object sender, EventArgs e)
        {
            DBManager t_db_mgr = new DBManager();
            int t_ret = -1;
            if (rdo_mysql.Checked)
            {
                t_ret=t_db_mgr.InitDBScript(Consts.DBType.MySQL, txt_link_string.Text.Trim());
            }
            else
            {
                t_ret=t_db_mgr.InitDBScript(Consts.DBType.ServerSQL, txt_link_string.Text.Trim());
            }

            if (t_ret !=-10)
            {
                MessageBox.Show("脚本执行成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("脚本执行失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
